package com.financing.platform.web.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.FpFileRecordDTO;
import com.financing.platform.service.IFpFileRecordService;

@Controller("fileController")
@RequestMapping("/api/fpFileRecord")
public class FileController  extends BaseAction{
	
	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	IFpFileRecordService fpFileRecordService;
	
	/**
	 *附件查询
	 * 
	 * @param request
	 * @param response
	 * @param refid
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list.shtml")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, HttpServletResponse response){
		ResultMap result = new ResultMap();
		try {
			logger.info("query fpFileRecord list begin .......");

			String refid = request.getParameter("refid");
			String type = request.getParameter("type");
			
			FpFileRecordDTO f = new FpFileRecordDTO();
			f.setRefid(refid);
			f.setType(type);
			List<FpFileRecordDTO> list = fpFileRecordService.queryList(f);

			result.setAttribute("list", list);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("query fpFileRecord list end .......");
		} catch (Exception e) {
			log.error("query fpFileRecord error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return result;
	}
	
	/**
	 * 删除附件
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete.shtml")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, HttpServletResponse response){
		ResultMap result = new ResultMap();
		try {
			logger.info("query fpFileRecord list begin .......");
			
			String id = request.getParameter("id");
			if(StringUtils.isEmpty(id)) {
				resultMap.setStatus(STATUS.FAILE);//参数缺失
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			fpFileRecordService.delete(Integer.valueOf(id));

			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("query fpFileRecord list end .......");
		} catch (Exception e) {
			log.error("query fpFileRecord error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return result;
	}

}
